#!/bin/sh -e

. CI/travis/lib.sh

# Don't prepare a deploy on a Coverity build
if [ "x${COVERITY_SCAN_PROJECT_NAME}" != "x" ] ; then exit 0; fi

deploy=0
if [ -z "$TRAVIS_BUILD_DIR" ] ; then
	t=$(find ./ -name CMakeCache.txt|head -1)
	if [ -n "${t}" ] ; then
		cd $(dirname $(dirname ${t}))
		TRAVIS_BUILD_DIR=$(pwd)
	else
		echo "I am confused - can't find CMakeCache.txt"
		exit
	fi
else
	cd $TRAVIS_BUILD_DIR
fi
pwd

if [ -z "${LDIST}" -a -f "build/.LDIST" ] ; then
	export LDIST="-$(cat build/.LDIST)"
fi
if [ -z "${LDIST}" ] ; then
	export LDIST="-$(get_ldist)"
fi

check_file()
{
temp=""
for i in $(find ./ -name CMakeCache.txt)
do
hit=$(find $(dirname ${i}) -maxdepth 1 -name "libiio*.$1" -a ! -name "*${LDIST}*")
if [ "$(echo ${hit} | wc -w)" -gt "1"  ] ; then
	echo "I am confused - more than 2 $1 files!"
	echo $hit
	exit 1
else
	if [ "$(echo ${hit} | wc -w)" -eq "1" ] ; then
		if [ -z "${temp}" ] ; then
			temp=$hit
		else
			echo "I am confused - more than 2 $1 files"
			echo $temp
			echo $hit
			exit 1
		fi
	fi
fi
done
}

check_file deb
if [ -n "${temp}" ] ; then
	deploy=$(expr ${deploy} + 1)
	if [ -z "${TARGET_DEB}" ] ; then
		export TARGET_DEB=$(echo ${temp} | \
			sed -e 's:^./.*/::' -e 's:-Linux::' -e 's:.deb$::')${LDIST}.deb
	fi
	echo "deploying ${temp} to nightly $TARGET_DEB"
	if [ -z "${RELEASE_PKG_FILE_DEB}" ] ; then
		export RELEASE_PKG_FILE_DEB=$(dirname ${temp})/${TARGET_DEB}
		cp ${temp} ${RELEASE_PKG_FILE_DEB}
	fi
	echo ${TARGET_DEB}
	ls -lh ${temp}
	echo ${RELEASE_PKG_FILE_DEB}
	ls -lh ${RELEASE_PKG_FILE_DEB}
else
	echo "Skipping deployment of debian package"
fi

check_file rpm
if [ -n "${temp}" ] ; then
	deploy=$(expr ${deploy} + 1)
	if [ -z "${TARGET_RPM}" ] ; then
		export TARGET_RPM=$(echo ${temp} | \
			sed -e 's:^./.*/::' -e 's:-Linux::' -e 's:.rpm$::')${LDIST}.rpm
	fi
	echo "deploying ${temp} to nightly $TARGET_RPM"
	if [ -z "${RELEASE_PKG_FILE_RPM}" ] ; then
		export RELEASE_PKG_FILE_RPM=$(dirname ${temp})/${TARGET_RPM}
		cp ${temp}  ${RELEASE_PKG_FILE_RPM}
	fi
	echo ${TARGET_RPM}
	ls -lh ${temp}
	echo ${RELEASE_PKG_FILE_RPM}
	ls -lh ${RELEASE_PKG_FILE_RPM}
else
	echo "Skipping deployment of rpm package"
fi

check_file tar.gz
if [  -n "${temp}"  ] ; then
	deploy=$(expr ${deploy} + 1)
	if [ -z "${TARGET_TGZ}" ] ; then
		echo updating tar file
		(
		cd $(dirname ${temp})
		if [ -d tarball_fixup ] ; then
			rm -rf tarball_fixup
		fi
		mkdir tarball_fixup && cd tarball_fixup

		if [ "$TRAVIS_OS_NAME" = "osx" ] ; then
			tar --strip-components=1 -xzf ${TRAVIS_BUILD_DIR}/${temp}
		else
			tar -xzf ${TRAVIS_BUILD_DIR}/${temp}
		fi

		if [ "$TRAVIS_OS_NAME" = "osx" ] ; then
			cd usr/lib
			ln -fs ../../Library/Frameworks/iio.framework/iio libiio.dylib
			install_name_tool -change /usr/local/opt/libusb/lib/libusb-1.0.0.dylib @rpath/libusb-1.0.dylib libiio.dylib
			install_name_tool -add_rpath @loader_path/../../../../../usr/lib libiio.dylib
			install_name_tool -add_rpath /usr/local/opt/libusb/lib libiio.dylib

			cd ../include
			ln -s ../../Library/Frameworks/iio.framework/Headers/iio.h iio.h

			# Update references for tools
			cd ../..
			TOOLS=Library/Frameworks/iio.framework/Tools/*
			for tool in $TOOLS
			do
				install_name_tool -add_rpath @loader_path/../../ $tool
			done
			cp /usr/local/lib/libusb-1.0.dylib usr/lib/
			chmod +w usr/lib/libusb-1.0.dylib
			install_name_tool -id @rpath/libusb-1.0.dylib usr/lib/libusb-1.0.dylib

			tar -czf ${TRAVIS_BUILD_DIR}/${temp} usr Library
		else
			tar -czf ${TRAVIS_BUILD_DIR}/${temp} usr lib
		fi
		)

		export TARGET_TGZ=$(echo ${temp} | \
			sed -e 's:^./.*/::' -e 's:-Linux::' -e 's:-Darwin::' -e 's:.tar.gz$::')${LDIST}.tar.gz;
	fi
	echo "deploying ${temp} to $TARGET_TGZ"
	if [ -z "${RELEASE_PKG_FILE_TGZ}" ] ; then
		export RELEASE_PKG_FILE_TGZ=$(dirname ${temp})/${TARGET_TGZ}
		cp ${temp} ${RELEASE_PKG_FILE_TGZ}
	fi
	echo ${TARGET_TGZ}
	ls -lh ${temp}
	echo ${RELEASE_PKG_FILE_TGZ}
	ls -lh ${RELEASE_PKG_FILE_TGZ}
else
	echo "Skipping deployment of tarball"
fi

check_file pkg
if [ -n "${temp}" ] ; then
	deploy=$(expr ${deploy} + 1)
	if [ -z "${TARGET_PKG}" ] ; then
		export TARGET_PKG=$(echo ${temp} | \
			sed -e 's:^./.*/::' -e 's:.pkg$::')${LDIST}.pkg
	fi
	echo "deploying ${temp} to nightly $TARGET_PKG"
	if [ -z "${RELEASE_PKG_FILE_PKG}" ] ; then
		export RELEASE_PKG_FILE_PKG=$(dirname ${temp})/${TARGET_PKG}
		cp ${temp} ${RELEASE_PKG_FILE_PKG}
	fi
	echo ${TARGET_PKG}
	ls -lh ${temp}
	echo ${RELEASE_PKG_FILE_PKG}
	ls -lh ${RELEASE_PKG_FILE_PKG}
else
	echo "Skipping deployment of OS X package"
fi

if [ "${deploy}" -eq "0" ] ; then
	echo did not deploy any files
	exit 1
fi
